<template>
  <page-header-wrapper>
    <a-card>
      <JeepaySearchForm :searchFunc="searchFunc" :resetFunc="() => { searchData = {} }">
        <jeepay-text-up v-model:value="searchData.configId" :placeholder="'厂商配置ID'" />
        <jeepay-text-up v-model:value="searchData.configDesc" :placeholder="'厂商配置备注'" />
        <a-form-item label="" class="table-search-item">
          <a-select v-model:value="searchData['provider']" placeholder="所属厂商">
            <a-select-option value="">全部</a-select-option>
            <a-select-option v-for="item in speakerList" :key="item.value" :value="item.value">{{ item.text }}</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item label="" class="table-search-item">
          <a-select v-model:value="searchData['state']" placeholder="状态">
            <a-select-option value="">全部</a-select-option>
            <a-select-option value="0">禁用</a-select-option>
            <a-select-option value="1">启用</a-select-option>
          </a-select>
        </a-form-item>
      </JeepaySearchForm>
      <!-- 列表渲染 -->
      <JeepayTable
        ref="infoTable"
        :init-data="true"
        :req-table-data-func="reqTableDataFunc"
        :table-columns="tableColumns"
        :search-data="searchData"
        row-key="configId"
        @btnLoadClose="btnLoading=false"
      >
        <template #topBtnSlot>
          <a-button v-if="$access('ENT_DEVICE_SPEAKER_ADD')" type="primary" @click="addFunc"><plus-outlined />新建</a-button>
        </template>

        <template #bodyCell="{ column, record }">
          <template v-if="column.key === 'state'">
            <JeepayTableColState :state="record.state" :showSwitchType="$access('ENT_DEVICE_SPEAKER_EDIT')" :onChange="(state) => { return updateState(record.configId, state)}" />
          </template>

          <template v-if="column.key === 'provider'">
            <a-tag :key="record.provider" color="processing">
              {{ (speakerList.find(item => item.value == record.provider) as any).text || '其他' }}
            </a-tag>
          </template>

          <template v-if="column.key === 'operation'">
            <a-button v-if="$access('ENT_DEVICE_SPEAKER_EDIT')" type="link" @click="editFunc(record)">修改</a-button>
            <a-button v-if="$access('ENT_DEVICE_SPEAKER_DEL')" type="link" style="color: red" @click="delFunc(record.configId)">删除</a-button>
          </template>
        </template>
      </JeepayTable>
    </a-card>
    <!-- 新增页面组件  -->
    <InfoAddOrEdit ref="infoAddOrEdit" :callbackFunc="searchFunc" />
  </page-header-wrapper>
</template>
<script setup lang="ts">
import { API_URL_DEVICE_PROVIDER, req, reqLoad } from '@/api/manage'
import InfoAddOrEdit from './CommonAddOrEdit.vue'
import { ref, reactive, getCurrentInstance } from 'vue'
import provider from '../provider.json'

const speakerList = provider.speaker

const { $infoBox, $access } = getCurrentInstance()!.appContext.config.globalProperties

const infoAddOrEdit = ref()
const infoTable = ref()
let tableColumns = reactive([
  { key: 'configId', title: '厂商配置ID', dataIndex: 'configId' },
  { key: 'provider', title: '设备厂商', dataIndex: 'provider' },
  { title: '厂商配置备注', dataIndex: 'configDesc' },
  { key: 'state', title: '状态' },
  { title: '创建日期', dataIndex: 'createdAt' },
  { title: '操作', fixed: 'right', key: 'operation', align: 'center'}
])

  let btnLoading = ref(false)
  let searchData:any = ref({})

  function reqTableDataFunc(params: any) { // 请求table接口数据
    return req.list(API_URL_DEVICE_PROVIDER, Object.assign({ deviceType: 1 }, params))
  }
  function searchFunc () { // 点击【查询】按钮点击事件
    btnLoading.value = true
    infoTable.value.refTable(true)
  }
  function addFunc () { // 业务通用【新增】 函数
    infoAddOrEdit.value.show({ deviceType: 1 })
  }
  function editFunc (record: any) { // 业务通用【修改】 函数
    infoAddOrEdit.value.show(record)
  }
  function delFunc (recordId: any) { // 业务通用【删除】 函数
    $infoBox.confirmDanger('确认删除？', '', () => {
      reqLoad.delById(API_URL_DEVICE_PROVIDER, recordId).then((res: any) => {
          infoTable.value.refTable(true)
          $infoBox.message.success('删除成功')
        })
    })
  }
  function updateState (recordId, state) { // 【更新状态】
      const title = state ? '确认启用？' : '确认禁用？'
      const content = state ? '' : '禁用后商户使用此配置的设备将无法使用！'
      return new Promise<void>((resolve, reject) => {
        $infoBox.confirmDanger(title, content, () => {
          return reqLoad.updateById(API_URL_DEVICE_PROVIDER, recordId, { state: state }).then(res => {
            searchFunc()
            resolve()
          }).catch(err => reject(err))
        },
          () => {
          reject(new Error())
        })
      })
    }
</script>
